//
//  OSUnfairLockDemo.m
//  MultiperThreadTest02
//
//  Created by FelixYin on 2020/4/5.
//  Copyright © 2020 FelixYin. All rights reserved.
//

#import "OSUnfairLockDemo.h"
#import <os/lock.h>

/*
 os_unfair_lock 在iOS 10之后才能使用
 用来取代OSSpinLock ，当其他线程进来的时候，不会出现等待的情况，而是出于休眠状态  解决了浪费CPU资源的问题
 
 通过汇编代码可以看到os_unfair_lock是一个互斥锁
 */
@interface OSUnfairLockDemo ()

@property (nonatomic,assign) os_unfair_lock moneyLock;
@property (nonatomic,assign) os_unfair_lock ticketLock;

@end

@implementation OSUnfairLockDemo


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.moneyLock = OS_UNFAIR_LOCK_INIT;
        self.ticketLock = OS_UNFAIR_LOCK_INIT;
    }
    return self;
}

- (void)saleTicket{
    os_unfair_lock_lock(&_ticketLock);
    [super saleTicket];
    os_unfair_lock_unlock(&_ticketLock);
}

- (void)saveMoney{
    os_unfair_lock_lock(&_moneyLock);
    [super saveMoney];
    os_unfair_lock_unlock(&_moneyLock);
}

- (void)drawMoney{
    os_unfair_lock_lock(&_moneyLock);
    [super drawMoney];
    os_unfair_lock_unlock(&_moneyLock);
}



@end
